package com.usf.jprovider.schema.selector.entry;

import javax.xml.bind.annotation.XmlSeeAlso;

import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.schema.selector.AbstractSelector;

@XmlSeeAlso({FirstEntry.class,
			 LastEntry.class,
			 RandomEntry.class,
			 NextEntry.class})
public abstract class AbstractEntrySelector extends AbstractSelector{

	public AbstractEntrySelector(){
		super();
	}
	
	public final String create(Environment env, JObjectNode node, String[] source) {
		int index = index(env, node, source.length);
		node.setEntryIndex(index);
		return source[index];
	}
	public final int check(Environment env, JObjectNode node, String[] source, String value) {
		int index = startIndex(env, node, source.length);
		int end = endIndex(env, node, source.length);
		while(index < end && !source[index].contains(value)) index++;
		node.setEntryIndex(index);
		return index;
	}
	public String selectQuery(Environment env, String query, String value){
		if(query.toLowerCase().contains("where"))	//TODO Lexiq
			query+=" AND ";
		else query+=" WHERE ";
		String column = query.split(" ")[1];
		query += column + " " + value;
		return selectQuery(env, query);
	}
	
	public abstract String selectField(Environment env, String table, String column);
	public abstract String selectField(Environment env, String table, String column, String queryValue);
	public abstract String selectQuery(Environment env, String query);
	
	
}
